<template>
  <view>
    <van-card v-for="item in goodsList" :key="item.goods_id" :num="item.goods_number"
      :price="item.goods_price | toFixed2" :title="item.goods_name" :thumb="item.goods_small_logo || defaultPic"
      :thumb-link="`/subpkj/goods_detail/goods_detail?goods_id=${item.goods_id}`" />
  </view>
</template>

<script>
  import {
    getGoods
  } from '@/api/goods.js'
  import toast from '../../untils/toast'
  export default {
    data() {
      return {
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
        queryObj: {
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        goodsList: [],
        total: 0,
        loading: false
      }
    },
    onLoad(options) {
      this.queryObj.cid = options.cid || '5'
      this.getGoods()
    },
    methods: {
      async getGoods() {
        try {
          this.loading = true
          uni.showLoading({
            title: '数据加载中....'
          })
          const res = await getGoods(this.queryObj)
          console.log(res)
          this.goodsList = [...this.goodsList, ...res.message.goods]
          this.total = res.message.total
        } finally {
          this.loading = false
          uni.hideLoading()
        }

      },
      // 触底的事件
      onReachBottom() {
        console.log('触底')
        if (this.queryObj.pagesize * this.queryObj.pagenum >= this.total) return toast({
          title: '数据到底了'
        })
        if (this.loading) return
        this.queryObj.pagenum++
        // 重新获取列表数据
        this.getGoods()
      },
      // 下拉刷新的事件
      async onPullDownRefresh() {
        // 1. 重置关键数据
        this.queryObj.pagenum = 1
        this.goodsList = []
        this.loading = false
        // 2. 重新发起请求
        await this.getGoods()
        uni.stopPullDownRefresh()
      }
    }
  }
</script>

<style>

</style>
